package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.CartForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/shopping")
public class CartController {

    @Reference(timeout = 3000,check = false,retries = 0)
    ICartService cartService;


    @GetMapping ("/carts")
    public ResponseData cartList(HttpServletRequest request) {
        String tokenStr = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);

        JSONObject jsonObject = JSON.parseObject(tokenStr);
        long uid = jsonObject.getLong("uid");

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);

        CartListByIdResponse cartList = cartService.getCartListById(cartListByIdRequest);

        if (!ShoppingRetCode.SUCCESS.getCode().equals(cartList.getCode())) {
            return new ResponseUtil<>().setErrorMsg(cartList.getMsg());
        }
        return new ResponseUtil<>().setData(cartList.getCartProductDtos());
    }


    @PostMapping("/carts")
    public ResponseData addToCart(@RequestBody CartForm cartForm) {
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(cartForm.getUserId());
        addCartRequest.setItemId(cartForm.getProductId());
        addCartRequest.setNum(cartForm.getProductNum());

        AddCartResponse addCartResponse = cartService.addToCart(addCartRequest);
        if (!ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())) {
            return new ResponseUtil<>().setErrorMsg(addCartResponse.getMsg());
        }
        return new ResponseUtil<>().setData(null);

    }


    @PutMapping("/carts")
    public ResponseData updateCart(@RequestBody CartForm cartForm) {
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setChecked(cartForm.getChecked());
        request.setItemId(cartForm.getProductId());
        request.setNum(cartForm.getProductNum());
        request.setUserId(cartForm.getUserId());
        UpdateCartNumResponse response = cartService.updateCartNum(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable long uid, @PathVariable long pid) {
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);

        DeleteCartItemResponse deleteCartItemResponse
                = cartService.deleteCartItem(deleteCartItemRequest);

        if (deleteCartItemResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(deleteCartItemResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());
    }

    @DeleteMapping("/items/{uid}")
    public ResponseData deleteCheckedCartItem(@PathVariable long uid) {
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);

        DeleteCheckedItemResposne deleteCheckedItemResposne =
                cartService.deleteCheckedItem(deleteCheckedItemRequest);

        if (deleteCheckedItemResposne.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(deleteCheckedItemResposne.getMsg());
        }
        return new ResponseUtil().setErrorMsg(deleteCheckedItemResposne.getMsg());
    }

    @PutMapping("/items")
    public ResponseData checkAllCartItems(@RequestBody CartForm cartForm) {
        CheckAllItemRequest request = new CheckAllItemRequest();
        request.setChecked(cartForm.getChecked());
        request.setUserId(cartForm.getUserId());
        CheckAllItemResponse response = cartService.checkAllCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }
}
